package com.cory.module.job.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cory.boot.base.BaseController;
import com.cory.boot.domain.bean.Result;
import com.cory.boot.utils.SysUtils;
import com.cory.module.job.domain.entity.JobExcelExport;
import com.cory.module.job.service.JobExcelExportService;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;

/**
 * @author xiongcong
 * @date 2023-05-09
 */
@Controller
@RequestMapping("admin/job/excel/export")
public class JobExcelExportController extends BaseController {
    @Autowired
    private JobExcelExportService jobExcelExportService;

    @RequestMapping("list")
    public String list(JobExcelExport param, Model model) {
        param.setTenantId(SysUtils.getLoginTenantId());
        param.setSystem(SysUtils.getSystem());

        super.startPage();
        List<JobExcelExport> list = jobExcelExportService.list(param);

        model.addAttribute("page", new PageInfo<>(list));
        model.addAttribute("param", param);
        return "admin/job/excel/export/list";
    }

    @RequestMapping("delete")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public Result<?> delete(Integer id, String ids) {
        List<Integer> idList = id != null ? Lists.newArrayList(id) :
                Arrays.asList(ids.split(",")).stream().filter(item -> StringUtils.isNotBlank(item))
                        .map(item -> Integer.valueOf(item)).collect(Collectors.toList());
        if (idList.size() > 0) {
            UpdateWrapper<JobExcelExport> wrapper = new UpdateWrapper<>();
            wrapper.in("id", idList);

            JobExcelExport update = new JobExcelExport();
            update.setState(9);
            jobExcelExportService.update(update, wrapper);
        }
        return Result.success("删除成功!");
    }
}
